package org.duoyi.striver.service.task;

import lombok.extern.slf4j.Slf4j;
import org.duoyi.striver.StriverApplication;
import org.duoyi.striver.consts.Area;
import org.duoyi.striver.consts.Picture;
import org.duoyi.striver.consts.TaskState;
import org.duoyi.striver.model.impl.Sleeper;
import org.duoyi.striver.service.common.CommonService;

@Slf4j
public class TreasureTask extends AbstractTask {

    @Override
    public void doTask() {
        before();
        if (this.state.equals(TaskState.FINISHED)) {
            return;
        }

        while (true) {
            Sleeper.sleep(3000);

            boolean fighting = CommonService.isFighting();

            boolean systemStore = CommonService.isSystemStore();

            boolean boothStore = CommonService.isBoothStore();

            boolean petStore = CommonService.isPetStore();

            if (Picture.宝图任务_掌柜结束.find()) {
                Area.宝图任务_关闭掌柜结束语.leftClick();
                break;
            }
        }
    }

    public void before() {
        synchronized (StriverApplication.class) {
            //打开日程
            CommonService.openDailyPanel();
            //选择宝图任务
            if (Picture.宝图任务_已完成.find()) {
                this.state = TaskState.FINISHED;
                return;
            }
            //领取任务
            log.info("领取宝图任务");
            Area.宝图任务_日程领取.leftClick();
            //商人领取任务,可能跨服，增加睡眠
            Sleeper.sleep(2000);
            if (Picture.宝图任务_掌柜对话.find()) {
                log.info("掌柜对话领取任务");
                Area.宝图任务_掌柜领取.leftClick();
            } else {
                CommonService.openDailyPanel();
                Area.宝图任务_日程领取.leftClick();
                if (Picture.宝图任务_掌柜对话.find()) {
                    log.info("掌柜对话领取任务");
                    Area.宝图任务_掌柜领取.leftClick();
                }
            }
            this.state = TaskState.RUNNING;
        }
    }
}
